var pageii = null;
var grid = null;

$(function () {
    grid = lyGrid({
        pagId: 'paging',
        l_column: [{
            colkey: 'category',
            name: '类别',
            isSort: true,
        }, {
            colkey: 'title',
            name: '标题',
            isSort: true,
        }, {
            colkey: 'is_hidden',
            name: '是否隐藏',
            width: '90px',
            isSort: true,
            renderData: function (rowindex, data, rowdata, column) {
                var str = '';
                if (data == '0') {
                    str = '否';
                } else if (data == '1') {
                    str = '是';
                }
                var id = rowdata.id;
                var status = rowdata.is_hidden;
                return '<a href="javascript:toggleIsHidden(' + id + ',' + status + ');">' + str + '</a>';
            }
        }, {
            colkey: 'created_on',
            name: '创建时间',
            isSort: true
        }],
        jsonUrl: rootPath + '/sys/content/findByPage.shtml',
        dymCol: true,
        checkbox: true,
        serNumber: true
    });
    $('#search').click('click', function () { // 绑定查询按扭
        var searchParams = $('#searchForm').serializeJson(); // 初始化传参数
        grid.setOptions({
            data: searchParams
        });
    });

    $("#addSysContent").click("click", function () {
        addSysContent();
    });
    $("#editSysContent").click("click", function () {
        editSysContent();
    });
    $("#delSysContent").click("click", function () {
        delSysContent();
    });
});

function editSysContent() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }
    pageii = layer.open({
        title: "编辑",
        type: 2,
        area: ["600px", "80%"],
        content: rootPath + '/sys/content/editUI.shtml?id=' + cbox
    });
}

function addSysContent() {
    pageii = layer.open({
        title: "新增",
        type: 2,
        area: ["600px", "80%"],
        content: rootPath + '/sys/content/addUI.shtml'
    });
}

function delSysContent() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {
        layer.msg("请选择删除项！！");
        return;
    }
    layer.confirm('是否删除？', function (index) {
        var url = rootPath + '/sys/content/deleteEntity.shtml';
        var s = CommnUtil.ajax(url, {
            ids: cbox.join(",")
        }, "json");
        if (s == "success") {
            layer.msg('删除成功');
            grid.loadData();
        } else {
            layer.msg('删除失败');
        }
    });
}

function toggleIsHidden(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/sys/content/editIsHidden.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}
